package com.apache.auditRule.controller;

import java.sql.Clob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.auditRule.entity.Audit;
import com.apache.auditRule.manager.AuditManager;
import com.apache.auditRule.manager.AuditRuleManager;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.member.common.ConstantData;
import com.apache.uct.common.PBOSSOTools;

/**
 * 
 * description:  审核公共方法
 */
@Controller
@RequestMapping("/audit")
public class AuditController {

	@Autowired
	private AuditManager auditManager;

	@Autowired
	private AuditRuleManager auditRuleManager;

	@RequestMapping("getOpinion.action")
	@ResponseBody
	public Map<String, Object> getOpinion(String pid, String auditsStatus, String num, HttpServletRequest request) {
		ParamsVo<Audit> vo = new ParamsVo<Audit>();
		Audit audit = new Audit();
		audit.setPid(pid);
		audit.setAuditsStatus(auditsStatus);
		audit.setNum(num);
		vo.setObj(audit);
		Map<String, Object> result = new HashMap<String, Object>();
		List<Audit> list = auditManager.getList(vo);
		result.put("total", list.size());
		result.put("rows", list);
		return result;
	}

	@RequestMapping("getAuditList.action")
	@ResponseBody
	public Map<String, Object> getAuditList(String status, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows) throws Exception {

		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败！");

		String auditType = request.getParameter("auditType");
		String num = request.getParameter("num");

		ParamsVo<Audit> vo = new ParamsVo<Audit>();
		String tableName = request.getParameter("tableName");
		String colName = request.getParameter("colName");

		if (Validator.isNotNull(tableName) && Validator.isNotNull(colName)) {
			vo.setParams("tableName", tableName);
			vo.setParams("colName", colName);

		} else {
			resultMsg.setMsg("tableName或者colName参数不能为null");
		}

		Audit audit = new Audit();
		audit.setAuditType(auditType);
		audit.setCheckUser(PBOSSOTools.getLoginUser(request).getUserEname());
		audit.setAuditsStatus(ConstantData.AuditStatus.AUDITING);
		audit.setNum(num);
		vo.setObj(audit);
		vo.setParams("pageSize", Validator.getDefaultStr(rows, "15"));
		vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
		vo.setMethodKey("getAuditList");
		Page pageList = (Page) auditManager.execute(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		List list = pageList.getPageObjects();
		for (int i = 0; i < list.size(); i++) {
			Map map = new HashMap<String, String>();
			map = (HashMap<String, String>) list.get(i);
			//			map.put("STR_MAX_1", oracleClob2Str((Clob) map.get("STR_MAX_1")));

			//去除大字段
			map.remove("STR_MAX_1");
			map.remove("STR_MAX_2");
		}

		mapData.put("total", pageList.getCount());
		mapData.put("rows", list);
		mapData.put("resultMsg", resultMsg);
		return mapData;
	}

	/**
	 * 公用的审核方法
	 * description:  
	 * @param request
	 * @param response
	 * @param auditVo
	 * @param auditFlag
	 * @param methodKey
	 * @return  
	 * @author cbqiu 
	 * @update 2015-8-20
	 */
	@RequestMapping("audit.action")
	@ResponseBody
	public ResultMsg audit(HttpServletRequest request, HttpServletResponse response, Audit auditVo, String auditFlag,
			String methodKey) {
		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败！");
		try {
			ParamsVo<Audit> vo = new ParamsVo<Audit>();

			vo.setParams("loginUser", PBOSSOTools.getLoginUser(request));
			vo.setInfoId(auditVo.getAuditsId());
			String pid = request.getParameter("pid");//关联主键
			vo.setParams("pid", pid);
			Audit audit = (Audit) auditManager.getInfoById(vo);
			if (!Validator.isEmpty(audit)) {
				String status = audit.getAuditsStatus();
				//判断当前审核项状态是否还在审核中
				if (Validator.isNotNull(status) && ConstantData.AuditStatus.AUDITING.equals(status)) {
					//设置当前审核人
					audit.setCheckUser(PBOSSOTools.getLoginUser(request).getUserEname());
					audit.setAuditOpinion(auditVo.getAuditOpinion());
					audit.setColName(auditVo.getColName());
					vo.setObj(audit);
					if (!Validator.isEmpty(audit)) {
						if (Validator.isNotNull(methodKey)) {
							vo.setMethodKey(methodKey);
						} else {
							vo.setMethodKey("member-identify-audit");
						}
						vo.setParams("auditFlag", auditFlag);
						if (auditManager.editInfo(vo)) {
							resultMsg.setFlag(ConstantData.FLAG_OK);
							resultMsg.setMsg("操作成功！");
						}
					}
				} else {
					resultMsg.setFlag(ConstantData.FLAG_OK);
					resultMsg.setMsg("该记录的当前审核阶段已审核,将跳转到审核列表页面!");
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return resultMsg;

	}

	public static String oracleClob2Str(Clob clob) throws Exception {
		return (clob != null ? clob.getSubString(1, (int) clob.length()) : null);
	}

}
